package com.green.ioc;

import com.green.ioc.config.BeanConfig;
import com.green.ioc.entity.SttBook;
import com.green.ioc.entity.SttUser;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

//@ComponentScan(value = "com.green.ioc")
//@SpringBootApplication
public class SttApplication {
    public static void main(String[] args) {
//        SpringApplication.run(SttApplication.class, args);

        //1.通过xml文件获取Bean
        //spring4 都是用这种方式
//        ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("springContext.xml");
//        SttUser user1 = (SttUser) classPathXmlApplicationContext.getBean("user1");
//        System.out.println(user1);

        //通过文件系统获取
//        FileSystemXmlApplicationContext fileContext = new FileSystemXmlApplicationContext("C:\\Users\\greenCai\\Desktop\\code\\springboot-demo\\tempDoc\\springContext.xml");
//        SttUser user1 = (SttUser)fileContext.getBean("user1");
//        System.out.println(user1);

        //通过注解的方式，重点
//        AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext("com.green.ioc");
        //结合componentScan使用
//        AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(SttApplication.class);
//        SttBook sttBook1 = (SttBook) annotationConfigApplicationContext.getBean("sttBook");
//        SttBook sttBook2 = annotationConfigApplicationContext.getBean(SttBook.class);
//        System.out.println(sttBook1);
//        System.out.println(sttBook2);

        //通过@Bean注解获取，重点
        //结合componentScan使用
//        AnnotationConfigApplicationContext annotationContext1 = new AnnotationConfigApplicationContext(SttApplication.class);
        //通过扫描@Configuration注解，看看又没有这个Bean
        AnnotationConfigApplicationContext annotationContext1 = new AnnotationConfigApplicationContext(BeanConfig.class);
        Object myCar = annotationContext1.getBean("myCar");
        System.out.println(myCar);


    }


}
